package com.cucn.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cucn.eduservice.entity.EduCourse;
import com.cucn.eduservice.entity.EduCourseDescription;
import com.cucn.eduservice.entity.frontvo.CourseFrontVO;
import com.cucn.eduservice.entity.frontvo.CourseWebVO;
import com.cucn.eduservice.entity.vo.CourseInfoVO;
import com.cucn.eduservice.entity.vo.CoursePublishVO;
import com.cucn.eduservice.mapper.EduCourseMapper;
import com.cucn.eduservice.service.EduChapterService;
import com.cucn.eduservice.service.EduCourseDescriptionService;
import com.cucn.eduservice.service.EduCourseService;
import com.cucn.eduservice.service.EduVideoService;
import com.cucn.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author KouChaoJie
 * @since 2021-09-29
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    /**
     * 课程简介服务类
     */
    @Autowired
    private EduCourseDescriptionService courseDescriptionService;

    @Autowired
    private EduVideoService videoService;

    @Autowired
    private EduChapterService chapterService;

    /**
     * @param courseInfoVO 表单传递过来的课程基本信息传输对象
     */
    @Override
    public String saveCourseInfo(CourseInfoVO courseInfoVO) {
        //1.向课程表添加课程的基本信息
        //CourseInfoVO转换成EduCourse
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVO, eduCourse);
        int insert = baseMapper.insert(eduCourse);

        if (insert == 0) {
            throw new GuliException(20001, "添加课程信息失败");
        }

        //获取添加之后课程id
        String cid = eduCourse.getId();

        //2.向课程简介表添加课程简介edu_course_description
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setDescription(courseInfoVO.getDescription());
        courseDescription.setId(cid);
        courseDescriptionService.save(courseDescription);

        return cid;
    }

    /**
     * 根据课程id查询课程信息
     *
     * @param courseId 课程id
     * @return id对应的课程信息传输对象
     */
    @Override
    public CourseInfoVO getCourseInfo(String courseId) {
        //1.查询课程表
        EduCourse eduCourse = baseMapper.selectById(courseId);
        CourseInfoVO courseInfoVO = new CourseInfoVO();
        BeanUtils.copyProperties(eduCourse, courseInfoVO);

        //2.查询课程描述表
        EduCourseDescription courseDescription = courseDescriptionService.getById(courseId);
        courseInfoVO.setDescription(courseDescription.getDescription());

        return courseInfoVO;
    }

    /**
     * 修改课程信息方法
     *
     * @param courseInfoVO 前台传输过来的课程信息
     */
    @Override
    public void updateCourseInfo(CourseInfoVO courseInfoVO) {
        //修改课程表
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVO, eduCourse);
        int update = baseMapper.updateById(eduCourse);

        if (update == 0) {
            throw new GuliException(20001, "修改课程信息失败");
        }

        //修改课程描述表
        EduCourseDescription description = new EduCourseDescription();
        description.setId(courseInfoVO.getId());
        description.setDescription(courseInfoVO.getDescription());
        courseDescriptionService.updateById(description);
    }

    /**
     * 根据课程id查询课程最终确认信息
     *
     * @param courseId 最终确认课程的id
     * @return 最终课程信息传输对象
     */
    @Override
    public CoursePublishVO publishCourseInfo(String courseId) {
        return baseMapper.getPublishCourseInfo(courseId);
    }

    /**
     * 需要将视频，小节，章节，描述，课程本身都删除
     *
     * @param courseId 要删除的课程对应id
     */
    @Override
    public void removeCourse(String courseId) {
        //1.根据课程id删除小节
        videoService.removeVideoByCourseId(courseId);

        //2.根据课程id删除章节
        chapterService.removeChapterByCourseId(courseId);

        //3.根据课程id删除描述
        courseDescriptionService.removeById(courseId);

        //4.根据课程id删除课程本身
        int result = baseMapper.deleteById(courseId);
        //删除失败
        if (result == 0) {
            throw new GuliException(20001, "删除失败");
        }
    }

    /**
     * 查询前8条热门课程
     * Redis缓存
     *
     * @return 热门课程集合
     */
    @Override
    @Cacheable(value = "course", key = "'selectCourseList'")
    public List<EduCourse> selectCourseList() {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        wrapper.last("limit 8");
        return baseMapper.selectList(wrapper);
    }

    /**
     * 前台条件查询带分页查询课程
     *
     * @param pageParam    分页对象
     * @param courseFrontVO 条件查询封装对象
     * @return 查询出的所有分页信息
     */
    @Override
    public Map<String, Object> getCourseFrontList(Page<EduCourse> pageParam, CourseFrontVO courseFrontVO) {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        //判断条件值是否为空,不为空就拼接条件
        //判断一级分类
        if (!StringUtils.isEmpty(courseFrontVO.getSubjectParentId())) {
            wrapper.eq("subject_parent_id",courseFrontVO.getSubjectParentId());
        }
        if (!StringUtils.isEmpty(courseFrontVO.getSubjectId())) {
            wrapper.eq("subject_id",courseFrontVO.getSubjectId());
        }

        //关注度排序
        if (!StringUtils.isEmpty(courseFrontVO.getBuyCountSort())) {
            wrapper.orderByDesc("buy_count",courseFrontVO.getBuyCountSort());
        }
        //时间排序
        if (!StringUtils.isEmpty(courseFrontVO.getGmtCreateSort())) {
            wrapper.orderByDesc("gmt_create");
        }
        //价格排序
        if (!StringUtils.isEmpty(courseFrontVO.getPriceSort())) {
            wrapper.orderByDesc("price");
        }

        baseMapper.selectPage(pageParam, wrapper);

        List<EduCourse> records = pageParam.getRecords();
        long current = pageParam.getCurrent();
        //总页数
        long pages = pageParam.getPages();
        //每页记录数
        long size = pageParam.getSize();
        //总记录数
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();
        boolean hasPrevious = pageParam.hasPrevious();

        //把分页数据获取出来,放到map集合中
        Map<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;
    }

    /**
     * 根据课程id查询课程的基本信息
     *
     * @param courseId 课程id
     * @return 课程基本信息
     */
    @Override
    public CourseWebVO getBaseCourseInfo(String courseId) {

        return baseMapper.getBaseCourseInfo(courseId);
    }
}
